<template>
  <div class="hot">
    <div v-if="hotlist.length > 0">
      <div class="seacher-hot">
        <h3>热门搜索</h3>
        <ul>
          <li
            v-for="word in hotlist"
            :key="word.first"
            @click.stop="$emit('search', word.first)"
          >
            {{ word.first }}
          </li>
        </ul>
      </div>
      <ol class="seacher-history">
        <transition-group
          leave-active-class="animate__animated animate__fadeOutRightBig"
        >
          <li
            @click.stop="seacherword(history)"
            v-for="history in historyArr"
            :key="history"
          >
            {{ history }}

            <i @click.stop="delhistory(history)"></i>
          </li>
        </transition-group>
      </ol>
    </div>
    <load-animate v-else></load-animate>
  </div>
</template>

<script>
export default {
  data() {
    return {
      hotlist: [],
      historyArr: [],
    };
  },
  created() {
    this.gethotlist();
  },
  methods: {
    async gethotlist() {
      await this.axios.get("/search/hot").then((res) => {
        this.hotlist = res.result.hots;
        this.gethistroy();
      });
    },
    gethistroy() {
      let historyword = localStorage.getItem("seacherhistory");
      if (historyword) {
        this.historyArr = JSON.parse(historyword);
      }
    },
    seacherword(word) {
      this.$emit("search", word);
    },
    delhistory(history) {
      let historywords = localStorage.getItem("seacherhistory");
      historywords = JSON.parse(historywords).filter((item) => {
        return item != history;
      });
      this.historyArr = historywords;
      localStorage.setItem("seacherhistory", JSON.stringify(historywords));
    },
  },
};
</script>

<style lang="scss" scoped>
.hot {
  background-color: #fcfcfd;
  .seacher-hot {
    padding: 15px 10px 0;
    h3 {
      font-size: 12px;
      color: #666;
    }
    ul {
      overflow: hidden;
      margin: 10px 0;
      li {
        float: left;
        height: 32px;
        line-height: 32px;
        padding: 0px 13px;
        margin: 0px 8px 8px 0px;
        border: 1px solid rgba(0, 0, 0, 0.1);
        border-radius: 16px;
      }
    }
  }
  .seacher-history {
    margin-left: 35px;
    li {
      line-height: 45px;
      font-size: 14px;
      position: relative;
      &::before {
        content: "";
        width: 15px;
        height: 15px;
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        left: -25px;
        background: url(../assets/images/history_icon.png) no-repeat;
        background-position: 0 0;
        background-size: contain;
        margin-top: 2px;
      }
      i {
        width: 12px;
        height: 12px;
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        right: 20px;
        background: url(../assets/images/del_icon.png) no-repeat;
        background-position: 0 0;
        background-size: contain;
      }
    }
  }
}
</style>
